home *** CD-ROM | disk | FTP | other *** search
/ Point Programming 1 / PPROG1.ISO / pascal / swag / printing.swg / 0017_GREAT Printer Unit.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-07-16  |  9.3 KB  |  257 lines

  1.  
  2.               (* Insert a '.' before the statment '$DEFINE' to        *)
  3.               (* compile without debugging information.               *)
  4. {.$DEFINE DebugMode}
  5.  
  6. {$IFDEF DebugMode}
  7.   {$A+,B-,D+,E-,F-,G-,I+,L+,N-,O-,P-,R+,S+,V+,X-}
  8. {$ELSE}
  9.   {$A+,B-,D-,E-,F-,G-,I-,L-,N-,O-,P-,R-,S-,V-,X-}
  10. {$ENDIF}
  11.  
  12. (**********************************************************************)
  13. (* PRINTIT.PAS - Public-domain TP printer unit by Guy McLoughlin.     *)
  14. (* version 1.10 (July, 1993)                                          *)
  15. (* Min TP version: 4+                                                 *)
  16. (**********************************************************************)
  17.  
  18. unit PrintIt;
  19.  
  20. (* BIT-MAP OF THE PRINTER "STATUS-BYTE"                               *)
  21. (* ------------------------------------                               *)
  22. (*                                                                    *)
  23. (* BIT NUMBER  7  6  5  4  3  2  1  0                                 *)
  24. (*             |  |  |  |  |  |  |  +-- Printer "timed-out"           *)
  25. (*             |  |  |  |  |  +--+----- These bits are NOT used       *)
  26. (*             |  |  |  |  +----------- Printer I/O error             *)
  27. (*             |  |  |  +-------------- Printer "selected"            *)
  28. (*             |  |  +----------------- Printer is out of paper       *)
  29. (*             |  +-------------------- Acknowlegment from printer    *)
  30. (*             +----------------------- Printer NOT busy              *)
  31.  
  32. interface
  33.  
  34. type
  35.   st_8 = string[8];
  36.  
  37.  
  38.   (***** Initialize printer port.                                     *)
  39.   (*                                                                  *)
  40.   function InitPrinterPort({ input} wo_PrinterNum : word) : {output} byte;
  41.  
  42.  
  43.   (***** Check the status of the printer.                             *)
  44.   (*                                                                  *)
  45.   function CheckPrinter({ input} wo_PrinterNum : word) : {output} byte;
  46.  
  47.  
  48.   (***** Initialize PrintIt variables, and check printer status.      *)
  49.   (*                                                                  *)
  50.   function InitPrintIt({ input}     st_PrinterID  : st_8;
  51.                                     by_PrinterNum : byte;
  52.                                     bo_InitPort   : boolean;
  53.                        {update} var fi_Printer    : text;
  54.                                 var by_Status     : byte)
  55.                        {output}   : boolean;
  56.  
  57.  
  58.   (***** Position printer "head" to X columns across, Y rows down.    *)
  59.   (*                                                                  *)
  60.   procedure P2xy({ input} var fi_Printer : text;
  61.                               by_Xaxis,
  62.                               by_Yaxis   : byte);
  63.  
  64.  
  65.   (***** Print string at position X columns across, Y rows down.      *)
  66.   (*                                                                  *)
  67.   procedure Pwrite({ input} var fi_Printer : text;
  68.                                 st_Data    : string;
  69.                                 by_Xaxis,
  70.                                 by_Yaxis   : byte);
  71.  
  72.  
  73. implementation
  74.  
  75. const         (* Line-feed, Carriage-return, Space character constant *)
  76.   co_Lf    = #10;
  77.   co_Cr    = #13;
  78.   co_Space = #32;
  79.  
  80. var           (* "space" character, and line-feed string variables.   *)
  81.   st_Spaces,
  82.   st_LineFeeds : string;
  83.  
  84.  
  85.   (***** Initialize printer port.                                     *)
  86.   (*                                                                  *)
  87.   function InitPrinterPort({ input} wo_PrinterNum : word) :
  88.                            {output} byte; assembler;
  89.   asm
  90.     mov ax, 0100h
  91.     mov dx, wo_PrinterNum
  92.     int 17h
  93.     mov al, ah
  94.   end;        (* InitPrinterPort.                                     *)
  95.  
  96.  
  97.   (***** Check the staus of the printer.                              *)
  98.   (*                                                                  *)
  99.   function CheckPrinter({ input} wo_PrinterNum : word) :
  100.                         {output} byte; assembler;
  101.   asm
  102.     mov ax, 0200h
  103.     mov dx, wo_PrinterNum
  104.     int 17h
  105.     mov al, ah
  106.   end;        (* CheckPrinter.                                        *)
  107.  
  108.  
  109.   (***** Initialize PrintIt variables, and check printer status.      *)
  110.   (*                                                                  *)
  111.   function InitPrintIt({ input}     st_PrinterID  : st_8;
  112.                                     by_PrinterNum : byte;
  113.                                     bo_InitPort   : boolean;
  114.                        {update} var fi_Printer    : text;
  115.                                 var by_Status     : byte)
  116.                        {output}   : boolean;
  117.   begin
  118.               (* Initialize "PrintIt" variables.                      *)
  119.     fillchar(st_Spaces, sizeof(st_Spaces), co_Space);
  120.     fillchar(st_LineFeeds, sizeof(st_LineFeeds), co_Lf);
  121.  
  122.               (* Try to open text-device printer variable.            *)
  123.     assign(fi_Printer, st_PrinterID);
  124.     {$I-}
  125.     rewrite(fi_Printer);
  126.     {$I+}
  127.     if (ioresult <> 0) then
  128.       begin
  129.         by_Status := $FF;
  130.         InitPrintIt := false
  131.       end
  132.     else
  133.       begin
  134.               (* Initialize printer-port if required.                 *)
  135.         if bo_InitPort then
  136.           by_Status := InitPrinterPort(by_PrinterNum)
  137.         else
  138.               (* Else, check the status of the printer.               *)
  139.           by_Status := CheckPrinter(by_PrinterNum);
  140.  
  141.               (* Check for error-flags in the printer status byte.    *)
  142.         if ((by_Status AND $29) = 0) then
  143.           InitPrintIt := true
  144.         else
  145.           InitPrintIt := false
  146.       end
  147.   end;        (* InitPrinter.                                         *)
  148.  
  149.  
  150.   (***** Position printer "head" to X columns across, Y rows down.    *)
  151.   (*                                                                  *)
  152.   procedure P2xy({ input} var fi_Printer : text;
  153.                               by_Xaxis,
  154.                               by_Yaxis   : byte);
  155.   begin
  156.     if (by_Yaxis > 0) then
  157.       begin
  158.         st_LineFeeds[0] := chr(by_Yaxis);
  159.         write(fi_Printer, st_LineFeeds)
  160.       end;
  161.     if (by_Xaxis > 0) then
  162.       begin
  163.         st_Spaces[0] := chr(pred(by_Xaxis));
  164.         write(fi_Printer, co_Cr + st_Spaces)
  165.       end
  166.   end;        (* P2xy.                                                *)
  167.  
  168.  
  169.   (***** Print string at position X columns across, Y rows down.      *)
  170.   (*                                                                  *)
  171.   procedure Pwrite({ input} var fi_Printer : text;
  172.                                 st_Data    : string;
  173.                                 by_Xaxis,
  174.                                 by_Yaxis   : byte);
  175.   begin
  176.     P2xy(fi_Printer, by_Xaxis, by_Yaxis);
  177.     write(fi_Printer, st_Data)
  178.   end;        (* Pwrite.                                              *)
  179.  
  180. END.
  181.  
  182. {--------------------------------   CUT HERE -----------------------------}
  183. (* Program to demo "PrintIt" unit.                    *)
  184.  
  185. program DemoPrintIt;
  186. uses
  187.   PrintIt;
  188.  
  189. const         (* Form-feed character.                               *)
  190.   co_FF = #12;
  191.  
  192. var           (* Printer "status" byte. Check "bit-map" in PrintIt  *)
  193.               (* unit for table of bit-flags.                       *)
  194.   by_PrinterStatus : byte;
  195.  
  196.               (* Our text-device interface variable.                *)
  197.   fi_Printer : text;
  198.  
  199.               (* Main program block.                                *)
  200. BEGIN
  201.               (* Initialize "PrintIt" variables, and check the      *)
  202.               (* status of the printer.                             *)
  203.   if NOT InitPrintIt('PRN', 0, false, fi_Printer, by_PrinterStatus) then
  204.  
  205.               (* InitPrintIt failed. Inform user of this, and halt. *)
  206.     begin
  207.       writeln('Error accessing printer!');
  208.       writeln('Printer error = ', by_PrinterStatus);
  209.       halt
  210.     end;
  211.               (* Print "SECRET" meaning of life symbol!!! <g>       *)
  212.               (* Position printer head to column 45, 5 rows down.   *)
  213.   P2xy(fi_Printer, 45, 5);
  214.  
  215.               (* Write some text to the printer.                    *)
  216.   write(fi_Printer, '_)');
  217.  
  218.   P2xy(fi_Printer, 43, 0);
  219.   write(fi_Printer, '(_');
  220.   P2xy(fi_Printer, 45, 1);
  221.   write(fi_Printer, '@)');
  222.   P2xy(fi_Printer, 43, 0);
  223.   write(fi_Printer, '(@');
  224.   P2xy(fi_Printer, 41, 1);
  225.   write(fi_Printer, '---\/');
  226.   P2xy(fi_Printer, 36, 0);
  227.   write(fi_Printer, '/----');
  228.   P2xy(fi_Printer, 35, 1);
  229.   write(fi_Printer, '/ |     ||');
  230.   P2xy(fi_Printer, 40, 1);
  231.   write(fi_Printer, '---||');
  232.   P2xy(fi_Printer, 34, 0);
  233.   write(fi_Printer, '*  ||-');
  234.   P2xy(fi_Printer, 37, 1);
  235.   write(fi_Printer, '^^    ^^');
  236.  
  237.               (* Print "SECRET" number code, using "Pwrite" routine.*)
  238.   Pwrite(fi_Printer, '10', 45, 5);
  239.   Pwrite(fi_Printer, '2', 37, 0);
  240.   Pwrite(fi_Printer, '8', 43, 0);
  241.   Pwrite(fi_Printer, '7', 42, 0);
  242.   Pwrite(fi_Printer, '1', 36, 0);
  243.   Pwrite(fi_Printer, '6', 41, 0);
  244.   Pwrite(fi_Printer, '3', 38, 0);
  245.   Pwrite(fi_Printer, '9', 44, 0);
  246.   Pwrite(fi_Printer, '5', 40, 0);
  247.   Pwrite(fi_Printer, '0', 35, 0);
  248.   Pwrite(fi_Printer, '4', 39, 0);
  249.  
  250.               (* Say good-bye, Guy.                                 *)
  251.   Pwrite(fi_Printer, '...Thats All Folks!!!', 30, 2);
  252.  
  253.               (* Send form-feed to printer.                         *)
  254.   write(fi_Printer, co_FF)
  255. END.
  256.  
  257.